##########################################################################################

library(data.table)
library(optparse)
library(parallel)

##########################################################################################

option_list <- list(
    make_option(c("--sample_list_file"), type = "character"),
    make_option(c("--sample_list_public_file"), type = "character"),
    make_option(c("--tmm_file"), type = "character"),
    make_option(c("--tmm_combine_file"), type = "character"),
    make_option(c("--tmm_combinepublic_file"), type = "character"),
    make_option(c("--out_path"), type = "character")
)

if(1!=1){
    
    sample_list_file <- "~/20220915_gastric_multiple/dna_combinePublic/config/tumor_normal.class.MSS_MSI.list"
    sample_list_public_file <- "~/20220915_gastric_multiple/dna_combinePublic/public_ref/combine/MutationInfo.combine.tsv"
    tmm_file <- "~/20220915_gastric_multiple/dna_combinePublic/mRNA/CombineCounts.FilterLowExpression.TMM.tsv"
    tmm_combine_file <- "~/20220915_gastric_multiple/dna_combinePublic/mRNA/CombineCounts.FilterLowExpression-MergeMutiSample.TMM.tsv"
    tmm_combinepublic_file <- "~/20220915_gastric_multiple/dna_combinePublic/mRNA/CombineCounts.TCGA_NJMU.FilterLowExpression.TMM.tsv"
    out_path <- "~/20220915_gastric_multiple/dna_combinePublic/mRNA"

}

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

sample_list_file <- opt$sample_list_file
sample_list_public_file <- opt$sample_list_public_file
tmm_file <- opt$tmm_file
tmm_combine_file <- opt$tmm_combine_file
tmm_combinepublic_file <- opt$tmm_combinepublic_file
out_path <- opt$out_path

##########################################################################################

info <- data.frame(fread(sample_list_file))
info_public <- data.frame(fread(sample_list_public_file))
info_public <- subset( info_public , MS_Type=="MSS" )
info_public$ID <- paste0( info_public$Tumor , "_" , info_public$Class )

dat_tpm <- data.frame(fread(tmm_file))
colnames(dat_tpm) <- gsub("[.]" , "-" , colnames(dat_tpm))
dat_tpm_combine <- data.frame(fread(tmm_combine_file))
dat_tpm_public_combine <- data.frame(fread(tmm_combinepublic_file))
colnames(dat_tpm_public_combine) <- gsub("[.]" , "-" , colnames(dat_tpm_public_combine))

## 去除TCGA的Normal
dat_tpm_public_combine <- dat_tpm_public_combine[ , grep( "Normal" , colnames(dat_tpm_public_combine) , invert = T )]

##########################################################################################
sid <- paste0(info$ID , "_" , info$Class_sub )
sid_normal <- paste0(info$ID , "_Normal" )
result <- dat_tpm[,colnames(dat_tpm) %in% c("gene_id" , sid , sid_normal)]
out_file <- paste0( out_path , "/" , "CombineTMM.DNAUse.MSS_MSI.tsv" )
write.table( result , out_file , row.names = F , sep = "\t" , quote = F )

sid <- unique(paste0(info$ID , "_" , info$Class ))
sid_normal <- paste0(info$ID , "_Normal" )
result_combine <- dat_tpm_combine[,colnames(dat_tpm_combine) %in% c("gene_id" , sid , sid_normal)]
out_file <- paste0( out_path , "/" , "CombineTMM.DNAUse.MergeMutiSample.MSS_MSI.tsv" )
write.table( result_combine , out_file , row.names = F , sep = "\t" , quote = F )